# Maintainer: Rui Abreu Ferreira <raf-ep@gmx.com>

_realname=mpack
pkgbase=mingw-w64-lua-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-lua51-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-lua-${_realname}")
pkgver=1.0.8
pkgrel=1
arch=('any')
url='https://github.com/libmpack/libmpack'
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-lua51"
             "${MINGW_PACKAGE_PREFIX}-lua"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-libmpack"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/libmpack/libmpack-lua/archive/${pkgver}.tar.gz")
sha256sums=('6d84d272bff27800ba1d7b718d1005ac6bfe35c5f3a5bf5d07e6ffd7e341d560')

build() {
  cd libmpack-lua-${pkgver}
  gcc -O2 -fPIC -DMPACK_USE_SYSTEM $(pkg-config --cflags lua) -shared -o mpack.dll lmpack.c $(pkg-config --libs lua) -lmpack
  gcc -O2 -fPIC -DMPACK_USE_SYSTEM $(pkg-config --cflags lua5.1) -shared -o mpack.dll.5.1 lmpack.c $(pkg-config --libs lua5.1) -lmpack
}

package_mingw-w64-lua-mpack() {
  pkgdesc='Msgpack serialization library for Lua (mingw-w64)'
  depends=("${MINGW_PACKAGE_PREFIX}-lua" "${MINGW_PACKAGE_PREFIX}-libmpack")

  cd libmpack-lua-${pkgver}
  local luaver=$(pkg-config lua --modversion | sed -r 's/^([0-9]+[.][0-9]+)[.][0-9]+$/\1/')
  install -Dm755 mpack.dll "${pkgdir}${MINGW_PREFIX}/lib/lua/${luaver}/mpack.dll"
  install -Dm644 LICENSE-MIT "${pkgdir}${MINGW_PREFIX}/share/licenses/lua-lmpack/LICENSE"
}

package_mingw-w64-lua51-mpack() {
  pkgdesc='Msgpack serialization library for Lua 5.1 (mingw-w64)'
  depends=("${MINGW_PACKAGE_PREFIX}-lua51" "${MINGW_PACKAGE_PREFIX}-libmpack")

  cd libmpack-lua-${pkgver}
  install -Dm755 mpack.dll.5.1 "${pkgdir}${MINGW_PREFIX}/lib/lua/5.1/mpack.dll"
  install -Dm644 LICENSE-MIT "${pkgdir}${MINGW_PREFIX}/share/licenses/lua51-mpack/LICENSE"
}

package_mingw-w64-i686-lua-mpack() {
  package_mingw-w64-lua-mpack
}

package_mingw-w64-x86_64-lua-mpack() {
    package_mingw-w64-lua-mpack
}

package_mingw-w64-i686-lua51-mpack() {
    package_mingw-w64-lua51-mpack
}

package_mingw-w64-x86_64-lua51-mpack() {
    package_mingw-w64-lua51-mpack
}
